﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TasksLibrary
{
    public delegate bool EventTaskMessageAdding<TMessType>(TaskMessage<TMessType> TaskMsg, TMessType message) where TMessType: new();

    public class TaskMessage
    {
        object[] mMessages = null;
        int mMessagePointer = 0;
        Type mMessType = null;

        public TaskMessage(int count, bool MessagesFromAnySender, Type messType)
        {
            mMessages = new object[count];
            AnySender = MessagesFromAnySender;
            mMessType = messType;
            Clear();
        }

        #region Messages
        public TaskableDsspServiceBase Service
        {
            get;
            set;
        }

        public bool IsComplete
        {
            get
            {
                if (mMessagePointer >= mMessages.Length) return true;
                return false;
            }
        }

        private bool AnySender
        {
            get;
            set;
        }

        public virtual void CompleteMessage(object message)
        {
            if (IsComplete) return;
            mMessages[mMessagePointer++] = message;
        }
        #endregion

        #region In
        public TMess Get<TMess>(int index) where TMess: new()
        {
            try
            {
                if (typeof(TMess) != mMessType) return default(TMess);
                if (index < 0 || index >= mMessagePointer) return default(TMess);
                return (TMess)mMessages[index];
            }
            catch (Exception) { }
            return default(TMess);
        }

        public int MessagePointer
        {
            get { return mMessagePointer; }
        }

        internal void Clear()
        {
            mMessagePointer = 0;
            for (int i = 0; i < mMessages.Length; i++)
                mMessages[i] = null;
        }
        #endregion
    }

    public class TaskMessage<TMessType>: TaskMessage where TMessType : new()
    {
        public TaskMessage(int count, bool MessagesFromAnySender)
            : base(count, MessagesFromAnySender, typeof(TMessType))
        { 

        }

        #region Events
        public event EventTaskMessageAdding<TMessType> TaskMessageAdding;
        #endregion

        #region Messages
        public override void CompleteMessage(object message)
        {
            if (IsComplete) return;
            if (message == null) return;
            if (message.GetType() != typeof(TMessType)) return;

            if (TaskMessageAdding != null)
                foreach (var _event in TaskMessageAdding.GetInvocationList())
                {
                    bool ret = false;
                    try
                    {
                        ret = (bool)_event.DynamicInvoke(this, message);
                        if (!ret) return;
                    }
                    catch (Exception)
                    { }
                }

            base.CompleteMessage(message);
        }

        public TMessType Get(int index)
        {
            return base.Get<TMessType>(index);
        }
        #endregion

    }
}
